<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2021/3/4 0004 18:21
 * Describe: 企业微信 - 消息管理类
 */

namespace app\common\server\corp;

use app\common\model\corp\Corp;

class Msg extends Base
{
    /**
     * @var string 发送欢迎语url
     */
    private $sendWelcomeMsgUrl = 'https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token=%s';

    /**
     * @var string 发送消息url
     */
    private $sendTextMsgUrl = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s';

    /**
     * 发送欢迎语事件
     * @link https://work.weixin.qq.com/api/doc/90001/90143/92599
     */
    public function sendWelcomeMsg(string $corpId, string $welcomeCode)
    {
        $welcomeMsg = (new Corp)->getCorpWelcomeMsgByCorpId($corpId);
        $welcomeMsg['welcome_code'] = $welcomeCode;
        $res = $this->curlFn(sprintf($this->sendWelcomeMsgUrl, (new Token())->getAuthCorpAccessToken($corpId)), $welcomeMsg, true);
        $str = $this->setResponse($res);
        trace('发送欢迎语');
        trace($str);
    }

    /**
     * -主动推送文本消息
     */
    public function sendTextMsg()
    {
        $res = $this->curlFn(sprintf($this->sendTextMsgUrl, 'r-VOeDw5SeXFcEaUWFXvzEfpO_wycK4pAeuuQHsXiYonyYqEFQK0-uLepgogBSSgeKoNiH9jK8v59SOrMGG6g0lLqZNsC8mKOqN6M4B7BtPPsRv-T6zDuG94xCZetzkwN5CrjNBeMI0Xkkl8po-MULLJ46F8GNC8vRPtMjt8NTCsybaJwpROJiAtF2uM4zSWTSybWD-pgE60grKQ0xuXmg'), [
            'touser' => 'txiaoWen',
            'msgtype' => 'text',
            'agentid' => '1000037',
            'text' => [
                'content' => 'xxxxxxxxxxxxxxxxxxxxx',
            ],
        ], true);
        dump($res);
        $str = $this->setResponse($res);
        dump($str);die;
    }

    /**
     * -（被动）回复文本消息
     */
    public function replyTextMsg()
    {

    }


}